package com.hex.ds.hdrs.common.utils;

/**
 * @Package: com.hex.ds.hdrs.common.utils
 * @ClassName SecToTimeUtil
 * @Description:秒转时分秒工具类
 * @Author: <gc.zuo>
 * @Date 2022/2/28 18:58
 * @Version v1.0
 **/
public class SecToTimeUtil {

    public static Duration secToTime(long time) {
        String timeStr = null;
        long hour = 0;
        long minute = 0;
        long second = 0;
        if (time <= 0) {
            return new Duration(0, 0, 0);
        } else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99) {
                    return new Duration(59, 59, 99);
                }
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return new Duration(Integer.parseInt(unitFormat(second)), Integer.parseInt(unitFormat(minute)), Integer.parseInt(unitFormat(hour)));
    }

    private static String unitFormat(long i) {
        String retStr = null;
        if (i >= 0 && i < 10) {
            retStr = "0" + i;
        } else {
            retStr = "" + i;
        }
        return retStr;
    }

    /**
     * @Method: getCostTs <br>
     * @Param: [start, end] <br>
     * @Return: java.lang.String <br>
     * @Description：获取耗时<br>
     * @Author： gc.zuo<br>
     * @Date： 2022/2/28 19:22 <br>
     * @Version： V1.0<br>
     */
    public static String getCostTs(String start, String end) {
        Duration duration = secToTime(HdrsDateUtil.getDistanceTimes(start, end)[3]);
        return (duration.getHour() == 0 ? "" : duration.getHour() + "小时") + (duration.getMinute() == 0 ? "" : duration.getMinute() + "分钟")
                + (duration.getSecond() == 0 ? "" : duration.getSecond() + "秒");
    }
}